package com.hr;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.sa.Employee;
import com.espirit.finance.core.TicketAware;

//@Entity
@DomainModel
//@Table(indexes={
//		@Index(name="idx_empacctterm", columnNames={"employee_id","acctterm_id"} )},appliesTo = "espirit_salaryticket")
public class SalaryTicket implements Serializable, TicketAware {
	private static final long serialVersionUID = 4244033108062448415L;

	private Integer salaryId;
	
	public Integer getId() {
		return salaryId;
	}
	public void setId(Integer id) {
		this.salaryId = id;
	}	
	
	public Integer getDeliveryTimes() {
		return deliveryTimes;
	}
	public void setDeliveryTimes(Integer deliveryTimes) {
		this.deliveryTimes = deliveryTimes;
	}
	public BigDecimal getLeaveAppHours() {
		return leaveAppHours;
	}
	public void setLeaveAppHours(BigDecimal leaveAppHours) {
		this.leaveAppHours = leaveAppHours;
	}
	
	public BigDecimal getRewardFaultAmount() {
		return rewardFaultAmount;
	}
	public void setRewardFaultAmount(BigDecimal rewardFaultAmount) {
		this.rewardFaultAmount = rewardFaultAmount;
	}
	public BigDecimal getOtHours() {
		return otHours;
	}
	public void setOtHours(BigDecimal otHours) {
		this.otHours = otHours;
	}
	
//	@ManyToOne
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	

//	@OneToMany(targetEntity = SalaryTicketLateItem.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<SalaryTicketLateItem> getLateItems() {
		return lateItems;
	}
	public void setLateItems(List<SalaryTicketLateItem> lateItems) {
		this.lateItems = lateItems;
	}

	public List<SalaryTicketLateItem> safeLateItems() {
		if (lateItems == null){
			lateItems = new  ArrayList<SalaryTicketLateItem>();
		}
		return lateItems;
	}
	
	public Integer getAbsentDays() {
		return absentDays;
	}
	public void setAbsentDays(Integer absentDays) {
		this.absentDays = absentDays;
	}


//	@OneToMany(targetEntity = SalaryTicketLeaveEarlyItem.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<SalaryTicketLeaveEarlyItem> getLeaveEarlyItems() {
		return leaveEarlyItems;
	}
	public void setLeaveEarlyItems(List<SalaryTicketLeaveEarlyItem> leaveEarlyItems) {
		this.leaveEarlyItems = leaveEarlyItems;
	}

	public List<SalaryTicketLeaveEarlyItem> safeLeaveEarlyItems() {
		if (leaveEarlyItems == null){
			leaveEarlyItems = new  ArrayList<SalaryTicketLeaveEarlyItem>();
		}
		return leaveEarlyItems;
	}
	
//	@ManyToOne
//	public AcctTerm getAcctTerm() {
//		return acctTerm;
//	}
//	public void setAcctTerm(AcctTerm acctTerm) {
//		this.acctTerm = acctTerm;
//	}
//
////	@ManyToOne
//	public Department getDepartment() {
//		return department;
//	}
//	public void setDepartment(Department department) {
//		this.department = department;
//	}
	
//	@ManyToOne
//	public User getCreated() {
//		return created;
//	}
//	public void setCreated(User created) {
//		this.created = created;
//	}
//
//	public Date getCreatedOn() {
//		return createdOn;
//	}
//	public void setCreatedOn(Date createdOn) {
//		this.createdOn = createdOn;
//	}

//	@OneToMany(targetEntity = LeaveAppItem.class ,fetch=FetchType.LAZY,  mappedBy = "parent")
	public List<LeaveAppItem> getLeaveAppItems() {
		return leaveAppItems;
	}
	public void setLeaveAppItems(List<LeaveAppItem> leaveAppItems) {
		this.leaveAppItems = leaveAppItems;
	}
	
	public Map<String, BigDecimal> getLeaveApps() {
		return leaveApps;
	}
	public Map<String, BigDecimal> safeLeaveApps() {
		if (leaveApps == null) {
			leaveApps = new HashMap<String, BigDecimal>();
		}
		return leaveApps;
	}
	public void setLeaveApps(Map<String, BigDecimal> leaveApps) {
		this.leaveApps = leaveApps;
	}

	public BigDecimal getSchedualHours() {
		return schedualHours;
	}
	public void setSchedualHours(BigDecimal schedualHours) {
		this.schedualHours = schedualHours;
	}

	public BigDecimal getDutyHours() {
		return dutyHours;
	}
	public void setDutyHours(BigDecimal dutyHours) {
		this.dutyHours = dutyHours;
	}
	public Integer getDutyForgetTimes() {
		return dutyForgetTimes;
	}
	public void setDutyForgetTimes(Integer dutyForgetTimes) {
		this.dutyForgetTimes = dutyForgetTimes;
	}
	public Integer getDutySubstituteTimes() {
		return dutySubstituteTimes;
	}
	public void setDutySubstituteTimes(Integer dutySubstituteTimes) {
		this.dutySubstituteTimes = dutySubstituteTimes;
	}



	private Map<String, BigDecimal> leaveApps;
	private BigDecimal rewardFaultAmount;
	private Employee employee;
	private List<SalaryTicketLateItem> lateItems ;
	private List<SalaryTicketLeaveEarlyItem> leaveEarlyItems;
//	private AcctTerm acctTerm;
//	private Department department;
//	private User created;
//	private Date createdOn;
	private List<LeaveAppItem> leaveAppItems;
	
	private Integer absentDays;
	private Integer deliveryTimes;
	private BigDecimal leaveAppHours;
	private BigDecimal otHours;
	private BigDecimal schedualHours;
	private BigDecimal dutyHours;
	private Integer dutyForgetTimes;//忘打卡
	private Integer dutySubstituteTimes;//代打卡
	
	private BigDecimal schedualDays; //排班天数
	private BigDecimal dutyDays; //实际上班天数
	private BigDecimal attendancePay; //出勤工资
	
	private BigDecimal daySalary;
	private BigDecimal halfDaySalary;
	private BigDecimal hourSalary;
	private Boolean fullSchedualed;
	
	public void addDutyHours(BigDecimal dutyHours) {
		if (this.dutyHours == null) {
			this.dutyHours = dutyHours;
		} else {
			this.dutyHours = this.dutyHours.add(dutyHours);
		}
	}
	
	public BigDecimal getAttendanceRate() {
		if (schedualHours == null || dutyHours == null) return null;
		if (schedualHours.compareTo(BigDecimal.ZERO) <= 0) schedualHours = BigDecimal.ONE;
		return dutyHours.divide(schedualHours, 6, BigDecimal.ROUND_HALF_DOWN);
	}
	
	public BigDecimal getDutyDays() {
		return dutyDays;
	}
	public void setDutyDays(BigDecimal dutyDays) {
		this.dutyDays = dutyDays;
	}
	public BigDecimal getSchedualDays() {
		return schedualDays;
	}
	public void setSchedualDays(BigDecimal schedualDays) {
		this.schedualDays = schedualDays;
	}
	public BigDecimal getAttendancePay() {
		return attendancePay;
	}
	public void setAttendancePay(BigDecimal attendancePay) {
		this.attendancePay = attendancePay;
	}
	public BigDecimal getDaySalary() {
		return daySalary;
	}
	public void setDaySalary(BigDecimal daySalary) {
		this.daySalary = daySalary;
	}
	public BigDecimal getHalfDaySalary() {
		return halfDaySalary;
	}
	public void setHalfDaySalary(BigDecimal halfDaySalary) {
		this.halfDaySalary = halfDaySalary;
	}
	public BigDecimal getHourSalary() {
		return hourSalary;
	}
	public void setHourSalary(BigDecimal hourSalary) {
		this.hourSalary = hourSalary;
	}
	
	
	public Integer getLateTimes(){
		if (this.getLateItems() ==null || this.getLateItems().isEmpty()) return null;
		return this.getLateItems().size();
	}
	
	public Integer getLateMinutes(){
		if (this.getLateItems() ==null || this.getLateItems().isEmpty()) return null;
		Integer total = 0;
		for(SalaryTicketLateItem item : this.lateItems){
			if (item.getMinutes() != null){
				total += item.getMinutes();
			}
		}
		return total;
	}
	public Boolean getFullSchedualed() {
		return fullSchedualed;
	}
	public void setFullSchedualed(Boolean fullSchedualed) {
		this.fullSchedualed = fullSchedualed;
	}
	
//	public Boolean getFullSchedualed() {
//		Boolean full = false;
//		if (dutyDays != null && schedualDays != null){
//			if (dutyDays.compareTo(schedualDays) >= 0){
//				full = true;
//			}
//		}
//		return full;
//	}
}
